package com.michael.study.customview.animation

import android.animation.*
import android.os.Bundle
import android.view.View
import android.view.animation.AccelerateInterpolator
import androidx.appcompat.app.AppCompatActivity
import com.michael.study.customview.R
import com.michael.study.customview.animation.evaluator.CharEvaluator
import com.michael.study.customview.databinding.ActivityObjectAnimatorBinding
import com.michael.study.library.utils.SystemUtil

class ObjectAnimatorActivity : AppCompatActivity(), View.OnClickListener {

    private lateinit var mBinding:ActivityObjectAnimatorBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityObjectAnimatorBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
        mBinding.btnValueAnimator1.setOnClickListener(this)
        mBinding.btnValueAnimator2.setOnClickListener(this)
        mBinding.btnObjectAnimator.setOnClickListener(this)
        mBinding.btnPropertyValuesHolder.setOnClickListener(this)
        mBinding.btnKeyFrame.setOnClickListener(this)
        mBinding.btnPlayTogether.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when(v?.id) {
            R.id.btn_value_animator_1 -> {
                doValueAnimator()
            }

            R.id.btn_value_animator_2 -> {
                doValueAnimatorSenior()
            }

            R.id.btn_object_animator -> {
                doObjectAnimator()
            }

            R.id.btn_property_values_holder -> {
                doPropertyValueHolder()
            }

            R.id.btn_key_frame -> {
                doKeyFrame()
            }

            R.id.btn_play_together -> {
                doPlayTogetherAnimator()
            }
        }
    }

    /**
     * ValueAnimator
     */
    private fun doValueAnimator() {
        var valueAnimator = ValueAnimator.ofInt(0,SystemUtil.dp2px(this,100))
        valueAnimator.addUpdateListener {
            var height = it.animatedValue
            var params = mBinding.view.layoutParams
            params.height = height as Int;
            mBinding.view.layoutParams = params
        }
        valueAnimator.duration = 1000
        valueAnimator.start()
    }

    /**
     * 插值器
     */
    private fun doValueAnimatorSenior() {
        var animator = ValueAnimator.ofObject(CharEvaluator(),Character('A'),Character('Z'))
        animator.addUpdateListener {
            var value = animator.animatedValue as Char
            mBinding.view.text = String.format("%c",value)
        }
        animator.duration = 2000
        animator.interpolator = AccelerateInterpolator()
        animator.start()
    }

    /**
     * ObjectAnimator
     */
    private fun doObjectAnimator() {
        var animator = ObjectAnimator.ofFloat(mBinding.view,"alpha",0.0f,1.0f)
        animator.duration = 1500
        animator.interpolator = AccelerateInterpolator()
        animator.start()
    }

    private fun doPropertyValueHolder() {
        val rotationHolder =
            PropertyValuesHolder.ofFloat("rotation", 60f, -60f, 40f, -40f, -20f, 20f, 10f, -10f, 0f)
        val colorHolder =
            PropertyValuesHolder.ofInt("backgroundColor", -0x1, -0xff01, -0x100, -0x1)
        var animation = ObjectAnimator.ofPropertyValuesHolder(mBinding.view, rotationHolder, colorHolder)
        animation.interpolator = AccelerateInterpolator()
        animation.duration = 1000
        animation.start()
    }

    /**
     * 如果去掉第0帧，将以第一个关键帧为起始位置
    *  如果去掉结束帧，将以最后一个关键帧为结束位置
    *  使用Keyframe来构建动画，至少要有两个或两个以上帧
     */
    private fun doKeyFrame() {
        // 关键帧 表示动画进度在多少时 动画所对应的数值
        var keyFrame1 = Keyframe.ofFloat(0.0f,0.0f)
        var keyFrame2 = Keyframe.ofFloat(0.1f,20.0f)
        var keyFrame3 = Keyframe.ofFloat(0.2f,-20.0f)
        var keyFrame4 = Keyframe.ofFloat(0.3f,20.0f)
        var keyFrame5 = Keyframe.ofFloat(0.4f,-20.0f)
        var keyFrame6 = Keyframe.ofFloat(0.5f,20.0f)
        var keyFrame7 = Keyframe.ofFloat(0.6f,-20.0f)
        var keyFrame8 = Keyframe.ofFloat(0.7f,20.0f)
        var keyFrame9 = Keyframe.ofFloat(0.8f,-20.0f)
        var keyFrame10 = Keyframe.ofFloat(0.9f,20.0f)
        var keyFrame11 = Keyframe.ofFloat(1.0f,0.0f)
        var holder = PropertyValuesHolder.ofKeyframe("rotation",keyFrame1,keyFrame2,keyFrame3,keyFrame4,keyFrame5,keyFrame6,keyFrame7,keyFrame8,keyFrame9,keyFrame10,keyFrame11);
        var animator = ObjectAnimator.ofPropertyValuesHolder(mBinding.view, holder)
        animator.duration = 2000
        animator.start()
    }

    private fun doPlayTogetherAnimator() {

        var scaleAnimation = ObjectAnimator.ofFloat(mBinding.view,"scaleX",0.0f,1.2f)
        var scaleYAnimation = ObjectAnimator.ofFloat(mBinding.view,"scaleY",0.0f,1.2f)
        var alphaAnimator = ObjectAnimator.ofFloat(mBinding.view,"alpha",0.2f,1.0f)
        var translateXAnimator = ObjectAnimator.ofInt(mBinding.view,"translationX",0,300)
        var translateYAnimator = ObjectAnimator.ofInt(mBinding.view,"translationY",0,300)

        var animationSet = AnimatorSet()
        animationSet.playSequentially(scaleAnimation,alphaAnimator,translateXAnimator,translateYAnimator);
        animationSet.duration = 3000
        animationSet.start()

    }
}